//
//  MeTableView.m
//  Cat
//
//  Created by lushitong on 2018/11/29.
//  Copyright © 2018年 lushitong. All rights reserved.
//

#import "MeTableView.h"

#import "HttpWebManager.h"
#import "HttpUserManager.h"

#import "MeTopTableViewCell.h"
#import "LoginViewController.h"
#import "AboutViewController.h"
#import "SuggestViewController.h"
#import "InviteViewController.h"
#import "MemberViewController.h"
#import "MeInfoTableViewCell.h"
#import "FlSetViewController.h"
#import "WebViewController.h"
#import "ShareManager.h"
#import "DetailsViewController.h"
#import "MainManager.h"

@interface MeTableView()

@property (nonatomic, strong) MeTopTableViewCell *operCell;

@end

@implementation MeTableView

- (void)setCellModelData:(id)cellModelData {
	
}

- (instancetype)initWithFrame:(CGRect)frame sections:(NSInteger)sections {
	if (self = [super initWithFrame:frame sections:sections]) {
		self.addRefresh = NO;
		self.backgroundColor = [UIColor whiteColor];
		self.tableView.backgroundColor = [UIColor whiteColor];
		self.tableView.showsVerticalScrollIndicator = NO;
		self.noFirstAutoRequest = YES;
		self.outerControlLoading = NO;
		[self loadView];
	}
	return self;
}

- (void)setUser:(FMDBUser *)user {
	_user = user;
	[self loadView];
	[self.tableView reloadData];
}

- (void)loadView {
	[self p_setupTopCell];
	[self p_setupInfoCell];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
	CGFloat minOffset = - 200.0;
	CGFloat offsetY = scrollView.contentOffset.y + StatusBarHeight;
	if (offsetY < minOffset) {
		scrollView.contentOffset = CGPointMake(0, minOffset - StatusBarHeight);
	} else if (offsetY < 0 && offsetY >= minOffset) {
		[self.operCell updateCoverWithOffset:offsetY];
	} else {
		[self.operCell updateCoverWithOffset:0];
	}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	UITableViewCell *sectionCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
	if (indexPath.section == 0) {
		MeTopTableViewCell *cell = (MeTopTableViewCell *)sectionCell;
		self.operCell = cell;
	} else {
		if (indexPath.row == 4) {
			sectionCell = [self cellWithoutAnything];
		} else if (indexPath.row == 3) {
			MeInfoTableViewCell *cell = (MeInfoTableViewCell *)sectionCell;
			if (UDToken.length == 0) {
				cell.isRedPoint = NO;
			} else {
				cell.isRedPoint = !UDRedPoint;
			}
			cell.bottomLineView.backgroundColor = [UIColor clearColor];
		}
	}
	return sectionCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	CGFloat cellHeight  = [super tableView:tableView heightForRowAtIndexPath:indexPath];
	if (indexPath.section == 0) {
		cellHeight = kQKFitModuleDecimal(118 + 137) + StatusBarSafeInsetHeight;
	} else if (indexPath.section == 1) {
		if (indexPath.row == 4) {
			cellHeight = kQKFitModuleDecimal(8);
		} else {
			cellHeight = kQKFitModuleDecimal(50);
		}
	}
	return cellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	
	if (indexPath.section == 0) {
		
	} else {
		if (UDToken.length == 0) {
			if (indexPath.row == 0 || indexPath.row == 3 || indexPath.row == 5 || indexPath.row == 8 || indexPath.row == 1 || indexPath.row == 6) {
				LoginViewController *login = [[LoginViewController alloc] init];
				[self.currentNavigationController pushViewController:login animated:YES];
				return;
			}
		}
		if (indexPath.row == 0) {
			DetailsViewController *details = [[DetailsViewController alloc] init];
			[self.currentNavigationController pushViewController:details animated:YES];
		} else if (indexPath.row == 1) {
			MemberViewController *member = [[MemberViewController alloc] init];
			[self.currentNavigationController pushViewController:member animated:YES];
		} else if (indexPath.row == 2) {
			WebViewController *web = [[WebViewController alloc] init];
			web.URL = [HttpWebManager getNewGuide];
			[self.currentNavigationController pushViewController:web animated:YES];
		} else if (indexPath.row == 3) {
			InviteViewController *invite = [[InviteViewController alloc] init];
			UDRedPoint = YES;
			[MainManager removeBadgeWithIndex:TabbarIndexTypeMe];
			[self.currentNavigationController pushViewController:invite animated:YES];
		} else if (indexPath.row == 5) {
			if (![ShareManager isInstallHaveQQ]) {
				[FLProgressHUD showMessage:@"请安装QQ"];
				return;
			} else {
				NSString *qq=[NSString stringWithFormat:@"mqq://im/chat?chat_type=wpa&uin=%@&version=1&src_type=web",@"2816512230"];
				NSURL *url = [NSURL URLWithString:qq];
				[[UIApplication sharedApplication] openURL:url options:nil completionHandler:^(BOOL success) {
					
				}];
			}
		} else if (indexPath.row == 6) {
			SuggestViewController *suggest = [[SuggestViewController alloc] init];
			[self.currentNavigationController pushViewController:suggest animated:YES];
		} else if (indexPath.row == 7) {
			AboutViewController *about = [[AboutViewController alloc] init];
			[self.currentNavigationController pushViewController:about animated:YES];
		} else if (indexPath.row == 8) {
			FlSetViewController *set = [[FlSetViewController alloc] init];
			[self.currentNavigationController pushViewController:set animated:YES];
		}
	}
}

- (UITableViewCell *)cellWithoutAnything {
	static NSString *identifier = @"QKProfileNothingCell";
	UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];
	if (!cell) {
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
	}
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	cell.contentView.backgroundColor = COLOR_WITH_HEX(0xF3F3F3);
	return cell;
}

- (void)p_setupTopCell {
	QKBaseTableCellModel *meTopCellData = [[QKBaseTableCellModel alloc] init];
	meTopCellData.isDynamic = NO;
	meTopCellData.cellClass = @"MeTopTableViewCell";
	meTopCellData.cellData = self.user;
	meTopCellData.section = 0;
	meTopCellData.cellHeight = kQKFitModule(118 + 137);
	[self addTableSectionData:meTopCellData];
}

- (void)p_setupInfoCell {
	QKBaseTableCellModel *meTopCellData = [[QKBaseTableCellModel alloc] init];
	meTopCellData.isDynamic = YES;
	meTopCellData.cellClass = @"MeInfoTableViewCell";
	meTopCellData.section = 1;
	meTopCellData.cellData = [self bottomArry];
	meTopCellData.cellHeight = kQKFitModule(50);
	[self addTableSectionData:meTopCellData];
}

- (NSArray *)bottomArry {
	return @[@{@"imageName":@"me_feed_back_pic",@"leftText":@"我的订单"},@{@"imageName":@"me_vip_info_pic",@"leftText":@"我的会员",@"rightText":@"普通会员"},
			@{@"imageName":@"me_quick_pic",@"leftText":@"快速入门",@"rightText":@"赚钱宝典"},
			@{@"imageName":@"me_invite_pic",@"leftText":@"邀请好友"},
			@{@"imageName":@"",@"leftText":@"",@"rightText":@""},
			 @{@"imageName":@"me_kefu",@"leftText":@"联系客服"},
			@{@"imageName":@"me_feed_back_pic",@"leftText":@"意见反馈"},
			@{@"imageName":@"me_about_pic",@"leftText":@"关于我们"},
			 @{@"imageName":@"me_set_pic",@"leftText":@"设置"}];
}

#pragma mark----- lazy init -----

- (MeTopTableViewCell *)operCell {
	if (!_operCell) {
		_operCell = [[MeTopTableViewCell alloc] init];
	}
	return _operCell;
}


@end
